/*
 Xern Entity Relocation Network
 Copyright (C) 2010-2011 Ronie Salgado <roniesalg@gmail.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

#ifndef XERN_STRUTILS_H
#define XERN_STRUTILS_H

#include <string>
#include "utf8.h"

namespace Xern
{
	namespace
	{
		// String2Wide selector
		template<int WS>
		struct S2W
		{
		};

		template<>
		struct S2W<2>
		{
			template<typename u16bit_iterator, typename octet_iterator>
			static u16bit_iterator convert(octet_iterator start, octet_iterator end,
					u16bit_iterator result)
			{
				return utf8::utf8to16(start, end, result);
			}
		};

		template<>
		struct S2W<4>
		{
			template<typename octet_iterator, typename u32bit_iterator>
			static u32bit_iterator convert(octet_iterator start, octet_iterator end,
					u32bit_iterator result)
			{
				return utf8::utf8to32(start, end, result);
			}
		};

		// Wide2String selector
		template<int WS>
		struct W2S
		{
		};

		template<>
		struct W2S<2>
		{
			template<typename u16bit_iterator, typename octet_iterator>
			static octet_iterator convert(u16bit_iterator start, u16bit_iterator end,
					octet_iterator result)
			{
				return utf8::utf16to8(start, end, result);
			}
		};

		template<>
		struct W2S<4>
		{
			template<typename octet_iterator, typename u32bit_iterator>
			static octet_iterator convert(u32bit_iterator start, u32bit_iterator end,
					octet_iterator result)
			{
				return utf8::utf32to8(start, end, result);
			}
		};

	};

	inline std::wstring string2wstring(const std::string &str)
	{
		std::wstring ret;
		S2W<sizeof(wchar_t)>::convert(str.begin(), str.end(), std::back_inserter(
				ret));
		return ret;
	}

	inline std::string wstring2string(const std::wstring &wstr)
	{
		std::string ret;
		W2S<sizeof(wchar_t)>::convert(wstr.begin(), wstr.end(), std::back_inserter(
				ret));
		return ret;
	}

};

#endif //XERN_STRUTILS_H
